home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c,comp.unix.programmer
- Subject: Re: Q: '\n' character
- Date: 3 Apr 1996 09:55:39 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4jue2rINNchh@keats.ugrad.cs.ubc.ca>
- References: <31616F63.481D@lava.weeg.uiowa.edu> <4jrvv3$ask@aimnet.aimnet.com>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
-
- In article <4jrvv3$ask@aimnet.aimnet.com>,
- Jonathan S. McNeill <jmcneill@aimnet.com> wrote:
- >In article <31616F63.481D@lava.weeg.uiowa.edu>,
- >Artur Wojdat <awojdat@lava.weeg.uiowa.edu> wrote:
- >
- >> Is there a function or some sort of way that I could remove '\n'
- >>charecter form the end of the string. I'm reading from two files, want to
- >>form one line of text and then have it printed out to stdout. I use fgets to
- >>read from the file and I noticed that it appends newline char at the end.
- >
- >Well, if you want ultimate control over your data, just use a loop and
- >get a character at a time. Disclaimer: the following is an example, has
-
- That is a sound plan...
-
- >not been tested, and I agree that there are about a million ways you could do
- >this.
- >
- >/*
- > * loop through characters until newline or end of file
- > */
- >while ( (ch = getc(stream)) != '\n' && ch != EOF ) {
- >
- > sprintf(buffer, "%s%c", buffer, ch);
- >}
-
- But the execution is severely botched up! My goodness, where did you learn your
- C?
-
- Using the buffer as the source and destination? Using sprintf() to do array
- manipulations?
-
- If you are going to be silly enough to use a fixed buffer for a job that may
- involve files containing lines of arbitrary length, at least use something
- like:
-
- buffer[i++] = ch;
- or *bufptr++ = ch;
-
- Please don't post nonsense to this newsgroup. It doesn't do anyone any good.
-
- April Fool's day was two days ago.
-
- >/*
- > * add a trailing nul character so that you can use string functions
- > */
- >sprintf(buffer, "%s\0");
-
- Oh dear. This will clobber the buffer, not append to it, you silly! And
- sprintf() already adds a zero character by itself. The 's' in its name stands
- for 'string', in case you didn't know.
-
- This problem requires no buffering beyond what is already performed in the
- standard IO library.
-
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <errno.h>
-
- void concatprint(FILE *f1, FILE *f2)
-
- /*
- * read lines from f1 and f2 simultaneously, and print them side by side
- * f1 and f2 must be valid streams open for reading.
- */
-
- {
- int c;
-
- do {
- if (!feof(f1) && !ferror(f1))
- while ((c = getc(f1)) != '\n' && c != EOF)
- putchar(c);
- if (!feof(f2) && !ferror(f2))
- while ((c = getc(f2)) != '\n' && c != EOF)
- putchar(c);
- putchar('\n');
- } while ((!feof(f1) && !ferror(f1)) || (!feof(f2) && !ferror(f2)));
- }
- --
-
-